/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pfc.generacion;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.ddlutils.model.Database;
import pfc.comun.IDEpfc;
import pfc.comun.Proyecto;
import pfc.modelocg.CGField;
import pfc.modelocg.CGForm;
import pfc.modelocg.CGModel;
import pfc.modeloer.ERAttrib;
import pfc.modeloer.ERModel;
import pfc.utils.Utils;

/**
 *
 * @author xandru
 */
public abstract class GeneradorServlet {
    Proyecto _proyecto;
    Database _dbmodel;
    ERModel _erModel;
    CGModel _cgModel;

    public GeneradorServlet(Proyecto pro, Database db, ERModel er, CGModel cg) {
        this._proyecto = pro;
        this._dbmodel = db;
        this._erModel = er;
        this._cgModel = cg;
    }


    public void generarServlet(File srcPath,CGForm form,String tipo) {

        String clase = tipo + StringUtils.capitalize(form.getEntityName());

        IDEpfc.logTime("generando servlet " + clase);

        ArrayList<String> imports = new ArrayList<String>();
        ArrayList<String> fields = new ArrayList<String>();
        ArrayList<String> cl = new ArrayList<String>();
        ArrayList<String> methods = new ArrayList<String>();

        imports.add("package " + _proyecto.nombre + ".servlet;\n");

        addImports(imports);
        //addField(fields);
        addConstructor(methods, clase);
        addPost(methods);
        addGet(methods);
        addRequest(methods,form);


        try {
            PrintWriter dto = new PrintWriter(new FileOutputStream(srcPath.getAbsolutePath() + File.separator + clase + ".java"));

            for (String line : imports) {
                dto.println(line);
            }
            dto.println("public class " + clase + " extends HttpServlet {");
            for (String line : fields) {
                dto.println(line);
            }
            for (String line : methods) {
                dto.println(line);
            }
            dto.println("}");

            dto.flush();
            dto.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void addImports(ArrayList<String> imports) {
        String[] aImportar = {"pfc.core.db.*"
                            , "pfc.core.web.*"
                            ,"java.util.Date"
                            ,"java.util.Properties"
                            ,"javax.servlet.ServletException"
                            ,"javax.servlet.http.HttpServlet"
                            ,"javax.servlet.http.HttpServletRequest"
                            ,"javax.servlet.http.HttpServletResponse"
                            ,"javax.servlet.http.HttpSession"
                            ,"java.io.PrintWriter"
                            ,"java.io.IOException"
                            ,"java.beans.PropertyDescriptor"
                            ,"org.apache.commons.beanutils.PropertyUtils"
                            ,"org.apache.commons.lang.StringUtils"
                            ,"java.util.Enumeration"
                            ,"java.util.List"
                            ,"java.util.ArrayList"
                            ,_proyecto.nombre + ".dto.*"
                            ,_proyecto.nombre + ".dao.*"
                            ,_proyecto.nombre + ".pkgen.*"
                            ,_proyecto.nombre + ".validator.Validator"
                            , "pfc.core.db.DataSourceFactory"
                            , "javax.sql.DataSource"
                            };

        for (String p: aImportar) {
            imports.add("import " + p + ";");
        }
        imports.add("\n");
    }

    private void addField(ArrayList<String> campos, CGField field) {
        ERAttrib a = field.getAttrib();
        campos.add("private " + Utils.getTipoJava(a.getTypeCode()) + " _" + a.getName() + ";");
        
    }

    private void addConstructor(ArrayList<String> methods, String clase) {
        methods.add("public " + StringUtils.capitalize(clase) + "() {}");
    }

    private void addPost(ArrayList<String> methods) {
        methods.add("protected void doPost(HttpServletRequest request, HttpServletResponse response)");
        methods.add("throws ServletException, IOException {");
        methods.add("processRequest(request, response);");
        methods.add("}");
    }

    private void addGet(ArrayList<String> methods) {
         methods.add("protected void doGet(HttpServletRequest request, HttpServletResponse response)");
        methods.add("throws ServletException, IOException {");
        methods.add("processRequest(request, response);");
        methods.add("}");
    }

    abstract void addRequest(ArrayList<String> methods,CGForm form);
       
    }
